<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in employeeList"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />

      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeesList } from '@/api/employees'
import { addDepartments, getDepartmentsDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'VueHrSkDialogForm',
  props: {
    id: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    originList: {
      required: true,
      type: Array
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      let validCodeList = this.originList.map(item => item.code)
      if (this.isEdit) {
        validCodeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (validCodeList.includes(value)) {
        callback(new Error('部门编码已存在'))
      } else {
        callback()
      }
    }
    const validName = (rule, value, callback) => {
      let existNameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        const current = this.originList.find(item => item.id === this.id)
        existNameList = this.originList.filter(item => item.pid === current.pid && item.id !== this.id).map(item => item.name)
      }
      if (existNameList.includes(value)) {
        callback(new Error('部门名称已存在'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employeeList: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入长度1-50的字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入长度1-50的字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [

          { required: true, message: '请选择部门负责人', trigger: ['blur', 'change'] }

        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 300, message: '请输入长度1-300的字符', trigger: 'blur' }
        ]
      }
    }
  },

  // watch: {
  //   'id': {
  //     handler: function(newVal) {
  //       if (this.isEdit) {
  //         this.loadDepartmentDetail()
  //       }
  //     },
  //     deep: true
  //   }
  // },

  created() {
    this.loadEmployeeList()
    if (this.isEdit) {
      this.loadDepartmentDetail()
    }
  },

  methods: {
    async loadEmployeeList() {
      const res = await getEmployeesList()
      // console.log(res)
      if (res.code === 10000) {
        this.employeeList = res.data
      }
    },
    async loadDepartmentDetail() {
      const res = await getDepartmentsDetail(this.id)
      // console.log(res)
      this.form = { ...res.data }
    },
    async hAdd() {
      this.form.pid = this.id
      const res = await addDepartments(this.form)
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      console.log(res)
    },
    async hEdit() {
      try {
        const res = await updateDepartments(this.form)
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.hEdit() : this.hAdd()
        this.$emit('success')
      })
    },

    hCancel() {
      this.$emit('close')
    },
    resetForm() {
      this.$refs.deptForm.resetFields()
    }
  }

}
</script>

